#include <sys/types.h>
#include <stdio.h>
#include <stdint.h>
#include <ctype.h>

static void dump_line(long addr, uint8_t* buf, int len)  
{  
	int i, pos;  
	char line[80+1];  

	// address field  
	pos = sprintf(line, "%08lX ", addr);  

	// hex content  
	for (i = 0; i < 16; ++i) {  
		if (i % (16/2) == 0)
			line[pos++] = ' '; // Insert a space  

		if (i < len)
			pos += sprintf(&line[pos], "%02x ", buf[i]);  
		else
			pos += sprintf(&line[pos], "   ");  
	}
	pos += sprintf(&line[pos], " |");  

	// printable content  
	for (i = 0; i < len; ++i)
		line[pos++] = isprint(buf[i]) ? buf[i] : '.';  

	sprintf(&line[pos], "|\n");  
	fprintf(stderr, "%s", line);  
}

void dump(const char *prefix, void *_data, int len)  
{  
	int i;
	uint8_t *buf = _data;

	printf("---- starts %s -----\n", prefix ? prefix : "");
	for (i = 0; i < (len/16); ++i)
		dump_line(16*i, &buf[16*i], 16);  

	// Dump remaining which len is not 16  
	if (len % 16)
		dump_line(16*i, &buf[16*i], len % 16);

	printf("---- ends %s -----\n", prefix ? prefix : "");
}

